from queue import Queue
from threading import Thread
import sys
from PoseStream import getPoseStream
from ParseGesture import parseGestures
from ReadConfig import readConfig
import MatchGestrue

def parseFSM(configs, is_file=True):
    fsms=None
    def parse(config):
        gestrues=readConfig(config, is_file)
        # print('G[{}]:\t{}'.format(config,gestrues))
        fsm_list, fail_list=parseGestures(gestrues)
        # print('F[{}]:\t{}'.format(config,fsm_list))
        # print('E[{}]:\t{}'.format(config,fail_list))
        return fsm_list, fail_list
    if type(configs) is list:
        fsms=[]
        for config in configs:
            fsms.append(parse(config))
    else:
        fsms=parse(configs)
    return fsms
        
# 用来表示终止的特殊对象
_sentinel = object()

poses_q = Queue()
t1 = Thread(target=MatchGestrue.matchGesture, args=(poses_q, _sentinel))
t2 = Thread(target=getPoseStream, args=(poses_q, _sentinel))

def start():
    t1.start()
    t2.start()

if __name__ == '__main__':
    args=list(sys.argv[1:])
    if not args:
        args.append('config.ini')
    multi_hand_fsms=parseFSM(args, is_file=True)
    multi_hand_fsms=[x[0] for x in multi_hand_fsms]
    MatchGestrue.updateFSMS(multi_hand_fsms)
    start()
else:
    # 自动退出 https://www.jb51.net/article/176573.htm
    t1.setDaemon(True)
    t2.setDaemon(True)
    

